package net.lab1024.sa.admin.config;

import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
public class FlowableConfig implements EngineConfigurationConfigurer<SpringProcessEngineConfiguration> {

    // 核心新增：创建名为 "applicationTaskExecutor" 的 AsyncTaskExecutor Bean，满足 Flowable 7 依赖
    @Bean(name = "applicationTaskExecutor")
    public AsyncTaskExecutor applicationTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 配置线程池基本参数（可根据需求调整）
        executor.setCorePoolSize(5);          // 核心线程数
        executor.setMaxPoolSize(10);          // 最大线程数
        executor.setQueueCapacity(25);        // 任务队列容量
        executor.setThreadNamePrefix("Flowable-"); // 线程名前缀（便于日志排查）
        executor.initialize();                // 初始化线程池
        return executor;
    }

    // 原有 Flowable 配置（字体、数据库更新等）
    @Override
    public void configure(SpringProcessEngineConfiguration config) {
        config.setDatabaseSchemaUpdate(SpringProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        config.setAsyncExecutorActivate(false);
        config.setActivityFontName("SimSun");
        config.setLabelFontName("SimSun");
        config.setAnnotationFontName("SimSun");
        config.setXmlEncoding("UTF-8");
    }
}